Source of the materials: Biopython cookbook (adapted) Status: Draft

Multiple Sequence Alignment objects {#chapter:Bio.AlignIO}

This chapter is about Multiple Sequence Alignments, by which we mean a collection of multiple sequences which have been aligned together – usually with the insertion of gap characters, and addition of leading or trailing gaps – such that all the sequence strings are the same length. Such an alignment can be regarded as a matrix of letters, where each row is held as a SeqRecord object internally.

We will introduce the MultipleSeqAlignment object which holds this kind of data, and the Bio.AlignIO module for reading and writing them as various file formats (following the design of the Bio.SeqIO module from the previous chapter). Note that both Bio.SeqIO and Bio.AlignIO can read and write sequence alignment files. The appropriate choice will depend largely on what you want to do with the data.

The final part of this chapter is about our command line wrappers for common multiple sequence alignment tools like ClustalW and MUSCLE.

Parsing or Reading Sequence Alignments

We have two functions for reading in sequence alignments, Bio.AlignIO.read() and Bio.AlignIO.parse() which following the convention introduced in Bio.SeqIO are for files containing one or multiple alignments respectively.

Using Bio.AlignIO.parse() will return an *iterator* which gives MultipleSeqAlignment objects. Iterators are typically used in a for loop. Examples of situations where you will have multiple different alignments include resampled alignments from the PHYLIP tool seqboot, or multiple pairwise alignments from the EMBOSS tools water or needle, or Bill Pearson’s FASTA tools.

However, in many situations you will be dealing with files which contain only a single alignment. In this case, you should use the Bio.AlignIO.read() function which returns a single MultipleSeqAlignment object.

Both functions expect two mandatory arguments:

  1. The first argument is a *handle* to read the data from, typically an open file (see Section [sec:appendix-handles]), or a filename.

  2. The second argument is a lower case string specifying the alignment format. As in Bio.SeqIO we don’t try and guess the file format for you! See http://biopython.org/wiki/AlignIO for a full listing of supported formats.

There is also an optional seq_count argument which is discussed in Section [sec:AlignIO-count-argument] below for dealing with ambiguous file formats which may contain more than one alignment.

A further optional alphabet argument allowing you to specify the expected alphabet. This can be useful as many alignment file formats do not explicitly label the sequences as RNA, DNA or protein – which means Bio.AlignIO will default to using a generic alphabet.

Single Alignments

As an example, consider the following annotation rich protein alignment in the PFAM or Stockholm file format:

# STOCKHOLM 1.0
#=GS COATB_BPIKE/30-81  AC P03620.1
#=GS COATB_BPIKE/30-81  DR PDB; 1ifl ; 1-52;
#=GS Q9T0Q8_BPIKE/1-52  AC Q9T0Q8.1
#=GS COATB_BPI22/32-83  AC P15416.1
#=GS COATB_BPM13/24-72  AC P69541.1
#=GS COATB_BPM13/24-72  DR PDB; 2cpb ; 1-49;
#=GS COATB_BPM13/24-72  DR PDB; 2cps ; 1-49;
#=GS COATB_BPZJ2/1-49   AC P03618.1
#=GS Q9T0Q9_BPFD/1-49   AC Q9T0Q9.1
#=GS Q9T0Q9_BPFD/1-49   DR PDB; 1nh4 A; 1-49;
#=GS COATB_BPIF1/22-73  AC P03619.2
#=GS COATB_BPIF1/22-73  DR PDB; 1ifk ; 1-50;
COATB_BPIKE/30-81             AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA
#=GR COATB_BPIKE/30-81  SS    -HHHHHHHHHHHHHH--HHHHHHHH--HHHHHHHHHHHHHHHHHHHHH----
Q9T0Q8_BPIKE/1-52             AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA
COATB_BPI22/32-83             DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA
COATB_BPM13/24-72             AEGDDP...AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA
#=GR COATB_BPM13/24-72  SS    ---S-T...CHCHHHHCCCCTCCCTTCHHHHHHHHHHHHHHHHHHHHCTT--
COATB_BPZJ2/1-49              AEGDDP...AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA
Q9T0Q9_BPFD/1-49              AEGDDP...AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA
#=GR Q9T0Q9_BPFD/1-49   SS    ------...-HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH--
COATB_BPIF1/22-73             FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA
#=GR COATB_BPIF1/22-73  SS    XX-HHHH--HHHHHH--HHHHHHH--HHHHHHHHHHHHHHHHHHHHHHH---
#=GC SS_cons                  XHHHHHHHHHHHHHHHCHHHHHHHHCHHHHHHHHHHHHHHHHHHHHHHHC--
#=GC seq_cons                 AEssss...AptAhDSLpspAT-hIu.sWshVsslVsAsluIKLFKKFsSKA
//

This is the seed alignment for the Phage_Coat_Gp8 (PF05371) PFAM entry, downloaded from a now out of date release of PFAM from http://pfam.sanger.ac.uk/. We can load this file as follows (assuming it has been saved to disk as “PF05371_seed.sth” in the current working directory):


In [1]:
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")

This code will print out a summary of the alignment:


In [2]:
print(alignment)


SingleLetterAlphabet() alignment with 7 rows and 52 columns
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRL...SKA COATB_BPIKE/30-81
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKL...SRA Q9T0Q8_BPIKE/1-52
DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRL...SKA COATB_BPI22/32-83
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPM13/24-72
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA Q9T0Q9_BPFD/1-49
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKL...SRA COATB_BPIF1/22-73

You’ll notice in the above output the sequences have been truncated. We could instead write our own code to format this as we please by iterating over the rows as SeqRecord objects:


In [3]:
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")
print("Alignment length %i" % alignment.get_alignment_length())


Alignment length 52

In [4]:
for record in alignment:
    print("%s - %s" % (record.seq, record.id))


AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA - COATB_BPIKE/30-81
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA - Q9T0Q8_BPIKE/1-52
DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA - COATB_BPI22/32-83
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA - COATB_BPM13/24-72
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA - COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA - Q9T0Q9_BPFD/1-49
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA - COATB_BPIF1/22-73

You could also use the alignment object’s format method to show it in a particular file format – see Section [sec:alignment-format-method] for details.

Did you notice in the raw file above that several of the sequences include database cross-references to the PDB and the associated known secondary structure? Try this:


In [5]:
for record in alignment:
    if record.dbxrefs:
        print("%s %s" % (record.id, record.dbxrefs))


COATB_BPIKE/30-81 ['PDB; 1ifl ; 1-52;']
COATB_BPM13/24-72 ['PDB; 2cpb ; 1-49;', 'PDB; 2cps ; 1-49;']
Q9T0Q9_BPFD/1-49 ['PDB; 1nh4 A; 1-49;']
COATB_BPIF1/22-73 ['PDB; 1ifk ; 1-50;']

To have a look at all the sequence annotation, try this:


In [6]:
for record in alignment:
    print(record)


ID: COATB_BPIKE/30-81
Name: COATB_BPIKE
Description: COATB_BPIKE/30-81
Database cross-references: PDB; 1ifl ; 1-52;
Number of features: 0
/start=30
/accession=P03620.1
/end=81
Per letter annotation for: secondary_structure
Seq('AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA', SingleLetterAlphabet())
ID: Q9T0Q8_BPIKE/1-52
Name: Q9T0Q8_BPIKE
Description: Q9T0Q8_BPIKE/1-52
Number of features: 0
/start=1
/accession=Q9T0Q8.1
/end=52
Seq('AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA', SingleLetterAlphabet())
ID: COATB_BPI22/32-83
Name: COATB_BPI22
Description: COATB_BPI22/32-83
Number of features: 0
/start=32
/accession=P15416.1
/end=83
Seq('DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA', SingleLetterAlphabet())
ID: COATB_BPM13/24-72
Name: COATB_BPM13
Description: COATB_BPM13/24-72
Database cross-references: PDB; 2cpb ; 1-49;, PDB; 2cps ; 1-49;
Number of features: 0
/start=24
/accession=P69541.1
/end=72
Per letter annotation for: secondary_structure
Seq('AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA', SingleLetterAlphabet())
ID: COATB_BPZJ2/1-49
Name: COATB_BPZJ2
Description: COATB_BPZJ2/1-49
Number of features: 0
/start=1
/accession=P03618.1
/end=49
Seq('AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA', SingleLetterAlphabet())
ID: Q9T0Q9_BPFD/1-49
Name: Q9T0Q9_BPFD
Description: Q9T0Q9_BPFD/1-49
Database cross-references: PDB; 1nh4 A; 1-49;
Number of features: 0
/start=1
/accession=Q9T0Q9.1
/end=49
Per letter annotation for: secondary_structure
Seq('AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA', SingleLetterAlphabet())
ID: COATB_BPIF1/22-73
Name: COATB_BPIF1
Description: COATB_BPIF1/22-73
Database cross-references: PDB; 1ifk ; 1-50;
Number of features: 0
/start=22
/accession=P03619.2
/end=73
Per letter annotation for: secondary_structure
Seq('FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA', SingleLetterAlphabet())

Sanger provide a nice web interface at http://pfam.sanger.ac.uk/family?acc=PF05371 which will actually let you download this alignment in several other formats. This is what the file looks like in the FASTA file format:

>COATB_BPIKE/30-81
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA
>Q9T0Q8_BPIKE/1-52
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA
>COATB_BPI22/32-83
DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA
>COATB_BPM13/24-72
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA
>COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA
>Q9T0Q9_BPFD/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA
>COATB_BPIF1/22-73
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA

Note the website should have an option about showing gaps as periods (dots) or dashes, we’ve shown dashes above. Assuming you download and save this as file “PF05371_seed.faa” then you can load it with almost exactly the same code:

from Bio import AlignIO
alignment = AlignIO.read("PF05371_seed.faa", "fasta")
print(alignment)

All that has changed in this code is the filename and the format string. You’ll get the same output as before, the sequences and record identifiers are the same. However, as you should expect, if you check each SeqRecord there is no annotation nor database cross-references because these are not included in the FASTA file format.

Note that rather than using the Sanger website, you could have used Bio.AlignIO to convert the original Stockholm format file into a FASTA file yourself (see below).

With any supported file format, you can load an alignment in exactly the same way just by changing the format string. For example, use “phylip” for PHYLIP files, “nexus” for NEXUS files or “emboss” for the alignments output by the EMBOSS tools. There is a full listing on the wiki page (http://biopython.org/wiki/AlignIO) and in the built in documentation (also online):


In [7]:
from Bio import AlignIO
help(AlignIO)


Help on package Bio.AlignIO in Bio:

NAME
    Bio.AlignIO - Multiple sequence alignment input/output as alignment objects.

DESCRIPTION
    The Bio.AlignIO interface is deliberately very similar to Bio.SeqIO, and in
    fact the two are connected internally.  Both modules use the same set of file
    format names (lower case strings).  From the user's perspective, you can read
    in a PHYLIP file containing one or more alignments using Bio.AlignIO, or you
    can read in the sequences within these alignmenta using Bio.SeqIO.
    
    Bio.AlignIO is also documented at http://biopython.org/wiki/AlignIO and by
    a whole chapter in our tutorial:
    
    * `HTML Tutorial`_
    * `PDF Tutorial`_
    
    .. _`HTML Tutorial`: http://biopython.org/DIST/docs/tutorial/Tutorial.html
    .. _`PDF Tutorial`: http://biopython.org/DIST/docs/tutorial/Tutorial.pdf
    
    Input
    -----
    For the typical special case when your file or handle contains one and only
    one alignment, use the function Bio.AlignIO.read().  This takes an input file
    handle (or in recent versions of Biopython a filename as a string), format
    string and optional number of sequences per alignment.  It will return a single
    MultipleSeqAlignment object (or raise an exception if there isn't just one
    alignment):
    
    >>> from Bio import AlignIO
    >>> align = AlignIO.read("Phylip/interlaced.phy", "phylip")
    >>> print(align)
    SingleLetterAlphabet() alignment with 3 rows and 384 columns
    -----MKVILLFVLAVFTVFVSS---------------RGIPPE...I-- CYS1_DICDI
    MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTL...VAA ALEU_HORVU
    ------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSL...PLV CATH_HUMAN
    
    For the general case, when the handle could contain any number of alignments,
    use the function Bio.AlignIO.parse(...) which takes the same arguments, but
    returns an iterator giving MultipleSeqAlignment objects (typically used in a
    for loop). If you want random access to the alignments by number, turn this
    into a list:
    
    >>> from Bio import AlignIO
    >>> alignments = list(AlignIO.parse("Emboss/needle.txt", "emboss"))
    >>> print(alignments[2])
    SingleLetterAlphabet() alignment with 2 rows and 120 columns
    -KILIVDDQYGIRILLNEVFNKEGYQTFQAANGLQALDIVTKER...--- ref_rec
    LHIVVVDDDPGTCVYIESVFAELGHTCKSFVRPEAAEEYILTHP...HKE gi|94967506|receiver
    
    Most alignment file formats can be concatenated so as to hold as many
    different multiple sequence alignments as possible.  One common example
    is the output of the tool seqboot in the PHLYIP suite.  Sometimes there
    can be a file header and footer, as seen in the EMBOSS alignment output.
    
    Output
    ------
    Use the function Bio.AlignIO.write(...), which takes a complete set of
    Alignment objects (either as a list, or an iterator), an output file handle
    (or filename in recent versions of Biopython) and of course the file format::
    
      from Bio import AlignIO
      alignments = ...
      count = SeqIO.write(alignments, "example.faa", "fasta")
    
    If using a handle make sure to close it to flush the data to the disk::
    
      from Bio import AlignIO
      alignments = ...
      with open("example.faa", "w") as handle:
        count = SeqIO.write(alignments, handle, "fasta")
    
    In general, you are expected to call this function once (with all your
    alignments) and then close the file handle.  However, for file formats
    like PHYLIP where multiple alignments are stored sequentially (with no file
    header and footer), then multiple calls to the write function should work as
    expected when using handles.
    
    If you are using a filename, the repeated calls to the write functions will
    overwrite the existing file each time.
    
    Conversion
    ----------
    The Bio.AlignIO.convert(...) function allows an easy interface for simple
    alignnment file format conversions. Additionally, it may use file format
    specific optimisations so this should be the fastest way too.
    
    In general however, you can combine the Bio.AlignIO.parse(...) function with
    the Bio.AlignIO.write(...) function for sequence file conversion. Using
    generator expressions provides a memory efficient way to perform filtering or
    other extra operations as part of the process.
    
    File Formats
    ------------
    When specifying the file format, use lowercase strings.  The same format
    names are also used in Bio.SeqIO and include the following:
    
      - clustal -   Output from Clustal W or X, see also the module Bio.Clustalw
        which can be used to run the command line tool from Biopython.
      - emboss    - EMBOSS tools' "pairs" and "simple" alignment formats.
      - fasta     - The generic sequence file format where each record starts with
        an identifer line starting with a ">" character, followed by
        lines of sequence.
      - fasta-m10 - For the pairswise alignments output by Bill Pearson's FASTA
        tools when used with the -m 10 command line option for machine
        readable output.
      - ig        - The IntelliGenetics file format, apparently the same as the
        MASE alignment format.
      - nexus     - Output from NEXUS, see also the module Bio.Nexus which can also
        read any phylogenetic trees in these files.
      - phylip    - Interlaced PHYLIP, as used by the PHLIP tools.
      - phylip-sequential - Sequential PHYLIP.
      - phylip-relaxed - PHYLIP like format allowing longer names.
      - stockholm - A richly annotated alignment file format used by PFAM.
    
    Note that while Bio.AlignIO can read all the above file formats, it cannot
    write to all of them.
    
    You can also use any file format supported by Bio.SeqIO, such as "fasta" or
    "ig" (which are listed above), PROVIDED the sequences in your file are all the
    same length.

PACKAGE CONTENTS
    ClustalIO
    EmbossIO
    FastaIO
    Interfaces
    NexusIO
    PhylipIO
    StockholmIO

FUNCTIONS
    convert(in_file, in_format, out_file, out_format, alphabet=None)
        Convert between two alignment files, returns number of alignments.
        
            - in_file - an input handle or filename
            - in_format - input file format, lower case string
            - output - an output handle or filename
            - out_file - output file format, lower case string
            - alphabet - optional alphabet to assume
        
        **NOTE** - If you provide an output filename, it will be opened which will
        overwrite any existing file without warning. This may happen if even the
        conversion is aborted (e.g. an invalid out_format name is given).
    
    parse(handle, format, seq_count=None, alphabet=None)
        Iterate over an alignment file as MultipleSeqAlignment objects.
        
        Arguments:
          - handle    - handle to the file, or the filename as a string
            (note older versions of Biopython only took a handle).
          - format    - string describing the file format.
          - alphabet  - optional Alphabet object, useful when the sequence type
            cannot be automatically inferred from the file itself
            (e.g. fasta, phylip, clustal)
          - seq_count - Optional integer, number of sequences expected in each
            alignment.  Recommended for fasta format files.
        
        If you have the file name in a string 'filename', use:
        
        >>> from Bio import AlignIO
        >>> filename = "Emboss/needle.txt"
        >>> format = "emboss"
        >>> for alignment in AlignIO.parse(filename, format):
        ...     print("Alignment of length %i" % alignment.get_alignment_length())
        Alignment of length 124
        Alignment of length 119
        Alignment of length 120
        Alignment of length 118
        Alignment of length 125
        
        If you have a string 'data' containing the file contents, use::
        
          from Bio import AlignIO
          from StringIO import StringIO
          my_iterator = AlignIO.parse(StringIO(data), format)
        
        Use the Bio.AlignIO.read() function when you expect a single record only.
    
    read(handle, format, seq_count=None, alphabet=None)
        Turns an alignment file into a single MultipleSeqAlignment object.
        
        Arguments:
          - handle    - handle to the file, or the filename as a string
            (note older versions of Biopython only took a handle).
          - format    - string describing the file format.
          - alphabet  - optional Alphabet object, useful when the sequence type
            cannot be automatically inferred from the file itself
            (e.g. fasta, phylip, clustal)
          - seq_count - Optional integer, number of sequences expected in each
            alignment.  Recommended for fasta format files.
        
        If the handle contains no alignments, or more than one alignment,
        an exception is raised.  For example, using a PFAM/Stockholm file
        containing one alignment:
        
        >>> from Bio import AlignIO
        >>> filename = "Clustalw/protein.aln"
        >>> format = "clustal"
        >>> alignment = AlignIO.read(filename, format)
        >>> print("Alignment of length %i" % alignment.get_alignment_length())
        Alignment of length 411
        
        If however you want the first alignment from a file containing
        multiple alignments this function would raise an exception.
        
        >>> from Bio import AlignIO
        >>> filename = "Emboss/needle.txt"
        >>> format = "emboss"
        >>> alignment = AlignIO.read(filename, format)
        Traceback (most recent call last):
            ...
        ValueError: More than one record found in handle
        
        Instead use:
        
        >>> from Bio import AlignIO
        >>> filename = "Emboss/needle.txt"
        >>> format = "emboss"
        >>> alignment = next(AlignIO.parse(filename, format))
        >>> print("First alignment has length %i" % alignment.get_alignment_length())
        First alignment has length 124
        
        You must use the Bio.AlignIO.parse() function if you want to read multiple
        records from the handle.
    
    write(alignments, handle, format)
        Write complete set of alignments to a file.
        
        Arguments:
          - alignments - A list (or iterator) of Alignment objects (ideally the
            new MultipleSeqAlignment objects), or (if using Biopython
            1.54 or later) a single alignment object.
          - handle    - File handle object to write to, or filename as string
            (note older versions of Biopython only took a handle).
          - format    - lower case string describing the file format to write.
        
        You should close the handle after calling this function.
        
        Returns the number of alignments written (as an integer).

DATA
    __docformat__ = 'restructuredtext en'
    print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...

FILE
    /home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py


Multiple Alignments

The previous section focused on reading files containing a single alignment. In general however, files can contain more than one alignment, and to read these files we must use the Bio.AlignIO.parse() function.

Suppose you have a small alignment in PHYLIP format:

5    6
Alpha     AACAAC
Beta      AACCCC
Gamma     ACCAAC
Delta     CCACCA
Epsilon   CCAAAC

If you wanted to bootstrap a phylogenetic tree using the PHYLIP tools, one of the steps would be to create a set of many resampled alignments using the tool bootseq. This would give output something like this, which has been abbreviated for conciseness:

5     6
Alpha     AAACCA
Beta      AAACCC
Gamma     ACCCCA
Delta     CCCAAC
Epsilon   CCCAAA
    5     6
Alpha     AAACAA
Beta      AAACCC
Gamma     ACCCAA
Delta     CCCACC
Epsilon   CCCAAA
    5     6
Alpha     AAAAAC
Beta      AAACCC
Gamma     AACAAC
Delta     CCCCCA
Epsilon   CCCAAC
...
    5     6
Alpha     AAAACC
Beta      ACCCCC
Gamma     AAAACC
Delta     CCCCAA
Epsilon   CAAACC

If you wanted to read this in using Bio.AlignIO you could use:


In [8]:
from Bio import AlignIO
alignments = AlignIO.parse("data/resampled.phy", "phylip")
for alignment in alignments:
    print(alignment)
    print("")


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-8-13e6344aed5e> in <module>()
      1 from Bio import AlignIO
      2 alignments = AlignIO.parse("data/resampled.phy", "phylip")
----> 3 for alignment in alignments:
      4     print(alignment)
      5     print("")

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py in parse(handle, format, seq_count, alphabet)
    348         raise TypeError("Need integer for seq_count (sequences per alignment)")
    349 
--> 350     with as_handle(handle, 'rU') as fp:
    351         # Map the file format to a sequence iterator:
    352         if format in _FormatToIterator:

/home/tiago_antao/miniconda/lib/python3.5/contextlib.py in __enter__(self)
     57     def __enter__(self):
     58         try:
---> 59             return next(self.gen)
     60         except StopIteration:
     61             raise RuntimeError("generator didn't yield") from None

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/File.py in as_handle(handleish, mode, **kwargs)
     88                 yield fp
     89         else:
---> 90             with open(handleish, mode, **kwargs) as fp:
     91                 yield fp
     92     else:

FileNotFoundError: [Errno 2] No such file or directory: 'data/resampled.phy'

As with the function Bio.SeqIO.parse(), using Bio.AlignIO.parse() returns an iterator. If you want to keep all the alignments in memory at once, which will allow you to access them in any order, then turn the iterator into a list:


In [10]:
from Bio import AlignIO
alignments = list(AlignIO.parse("data/resampled.phy", "phylip"))
last_align = alignments[-1]
first_align = alignments[0]


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-10-0ef98bef9604> in <module>()
      1 from Bio import AlignIO
----> 2 alignments = list(AlignIO.parse("data/resampled.phy", "phylip"))
      3 last_align = alignments[-1]
      4 first_align = alignments[0]

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py in parse(handle, format, seq_count, alphabet)
    348         raise TypeError("Need integer for seq_count (sequences per alignment)")
    349 
--> 350     with as_handle(handle, 'rU') as fp:
    351         # Map the file format to a sequence iterator:
    352         if format in _FormatToIterator:

/home/tiago_antao/miniconda/lib/python3.5/contextlib.py in __enter__(self)
     57     def __enter__(self):
     58         try:
---> 59             return next(self.gen)
     60         except StopIteration:
     61             raise RuntimeError("generator didn't yield") from None

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/File.py in as_handle(handleish, mode, **kwargs)
     88                 yield fp
     89         else:
---> 90             with open(handleish, mode, **kwargs) as fp:
     91                 yield fp
     92     else:

FileNotFoundError: [Errno 2] No such file or directory: 'data/resampled.phy'

Ambiguous Alignments {#sec:AlignIO-count-argument}

Many alignment file formats can explicitly store more than one alignment, and the division between each alignment is clear. However, when a general sequence file format has been used there is no such block structure. The most common such situation is when alignments have been saved in the FASTA file format. For example consider the following:

>Alpha
ACTACGACTAGCTCAG--G
>Beta
ACTACCGCTAGCTCAGAAG
>Gamma
ACTACGGCTAGCACAGAAG
>Alpha
ACTACGACTAGCTCAGG--
>Beta
ACTACCGCTAGCTCAGAAG
>Gamma
ACTACGGCTAGCACAGAAG

This could be a single alignment containing six sequences (with repeated identifiers). Or, judging from the identifiers, this is probably two different alignments each with three sequences, which happen to all have the same length.

What about this next example?

>Alpha
ACTACGACTAGCTCAG--G
>Beta
ACTACCGCTAGCTCAGAAG
>Alpha
ACTACGACTAGCTCAGG--
>Gamma
ACTACGGCTAGCACAGAAG
>Alpha
ACTACGACTAGCTCAGG--
>Delta
ACTACGGCTAGCACAGAAG

Again, this could be a single alignment with six sequences. However this time based on the identifiers we might guess this is three pairwise alignments which by chance have all got the same lengths.

This final example is similar:

>Alpha
ACTACGACTAGCTCAG--G
>XXX
ACTACCGCTAGCTCAGAAG
>Alpha
ACTACGACTAGCTCAGG
>YYY
ACTACGGCAAGCACAGG
>Alpha
--ACTACGAC--TAGCTCAGG
>ZZZ
GGACTACGACAATAGCTCAGG

In this third example, because of the differing lengths, this cannot be treated as a single alignment containing all six records. However, it could be three pairwise alignments.

Clearly trying to store more than one alignment in a FASTA file is not ideal. However, if you are forced to deal with these as input files Bio.AlignIO can cope with the most common situation where all the alignments have the same number of records. One example of this is a collection of pairwise alignments, which can be produced by the EMBOSS tools needle and water – although in this situation, Bio.AlignIO should be able to understand their native output using “emboss” as the format string.

To interpret these FASTA examples as several separate alignments, we can use Bio.AlignIO.parse() with the optional seq_count argument which specifies how many sequences are expected in each alignment (in these examples, 3, 2 and 2 respectively). For example, using the third example as the input data:


In [11]:
for alignment in AlignIO.parse(handle, "fasta", seq_count=2):
    print("Alignment length %i" % alignment.get_alignment_length())
    for record in alignment:
        print("%s - %s" % (record.seq, record.id))
    print("")


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-6e1f5efd5fb4> in <module>()
----> 1 for alignment in AlignIO.parse(handle, "fasta", seq_count=2):
      2     print("Alignment length %i" % alignment.get_alignment_length())
      3     for record in alignment:
      4         print("%s - %s" % (record.seq, record.id))
      5     print("")

NameError: name 'handle' is not defined

Using Bio.AlignIO.read() or Bio.AlignIO.parse() without the seq_count argument would give a single alignment containing all six records for the first two examples. For the third example, an exception would be raised because the lengths differ preventing them being turned into a single alignment.

If the file format itself has a block structure allowing Bio.AlignIO to determine the number of sequences in each alignment directly, then the seq_count argument is not needed. If it is supplied, and doesn’t agree with the file contents, an error is raised.

Note that this optional seq_count argument assumes each alignment in the file has the same number of sequences. Hypothetically you may come across stranger situations, for example a FASTA file containing several alignments each with a different number of sequences – although I would love to hear of a real world example of this. Assuming you cannot get the data in a nicer file format, there is no straight forward way to deal with this using Bio.AlignIO. In this case, you could consider reading in the sequences themselves using Bio.SeqIO and batching them together to create the alignments as appropriate.

Writing Alignments

We’ve talked about using Bio.AlignIO.read() and Bio.AlignIO.parse() for alignment input (reading files), and now we’ll look at Bio.AlignIO.write() which is for alignment output (writing files). This is a function taking three arguments: some MultipleSeqAlignment objects (or for backwards compatibility the obsolete Alignment objects), a handle or filename to write to, and a sequence format.

Here is an example, where we start by creating a few MultipleSeqAlignment objects the hard way (by hand, rather than by loading them from a file). Note we create some SeqRecord objects to construct the alignment from.


In [9]:
from Bio.Alphabet import generic_dna
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment

align1 = MultipleSeqAlignment([
             SeqRecord(Seq("ACTGCTAGCTAG", generic_dna), id="Alpha"),
             SeqRecord(Seq("ACT-CTAGCTAG", generic_dna), id="Beta"),
             SeqRecord(Seq("ACTGCTAGDTAG", generic_dna), id="Gamma"),
         ])

align2 = MultipleSeqAlignment([
             SeqRecord(Seq("GTCAGC-AG", generic_dna), id="Delta"),
             SeqRecord(Seq("GACAGCTAG", generic_dna), id="Epsilon"),
             SeqRecord(Seq("GTCAGCTAG", generic_dna), id="Zeta"),
         ])

align3 = MultipleSeqAlignment([
             SeqRecord(Seq("ACTAGTACAGCTG", generic_dna), id="Eta"),
             SeqRecord(Seq("ACTAGTACAGCT-", generic_dna), id="Theta"),
             SeqRecord(Seq("-CTACTACAGGTG", generic_dna), id="Iota"),
         ])

my_alignments = [align1, align2, align3]

Now we have a list of Alignment objects, we’ll write them to a PHYLIP format file:


In [10]:
from Bio import AlignIO
AlignIO.write(my_alignments, "my_example.phy", "phylip")


Out[10]:
3

And if you open this file in your favourite text editor it should look like this:

3 12
Alpha      ACTGCTAGCT AG
Beta       ACT-CTAGCT AG
Gamma      ACTGCTAGDT AG
 3 9
Delta      GTCAGC-AG
Epislon    GACAGCTAG
Zeta       GTCAGCTAG
 3 13
Eta        ACTAGTACAG CTG
Theta      ACTAGTACAG CT-
Iota       -CTACTACAG GTG

Its more common to want to load an existing alignment, and save that, perhaps after some simple manipulation like removing certain rows or columns.

Suppose you wanted to know how many alignments the Bio.AlignIO.write() function wrote to the handle? If your alignments were in a list like the example above, you could just use len(my_alignments), however you can’t do that when your records come from a generator/iterator. Therefore the Bio.AlignIO.write() function returns the number of alignments written to the file.

Note - If you tell the Bio.AlignIO.write() function to write to a file that already exists, the old file will be overwritten without any warning.

Converting between sequence alignment file formats {#sec:converting-alignments}

Converting between sequence alignment file formats with Bio.AlignIO works in the same way as converting between sequence file formats with Bio.SeqIO (Section [sec:SeqIO-conversion]). We load generally the alignment(s) using Bio.AlignIO.parse() and then save them using the Bio.AlignIO.write() – or just use the Bio.AlignIO.convert() helper function.

For this example, we’ll load the PFAM/Stockholm format file used earlier and save it as a Clustal W format file:


In [11]:
from Bio import AlignIO
count = AlignIO.convert("data/PF05371_seed.sth", "stockholm", "PF05371_seed.aln", "clustal")
print("Converted %i alignments" % count)


Converted 1 alignments

Or, using Bio.AlignIO.parse() and Bio.AlignIO.write():


In [12]:
from Bio import AlignIO
alignments = AlignIO.parse("data/PF05371_seed.sth", "stockholm")
count = AlignIO.write(alignments, "PF05371_seed.aln", "clustal")
print("Converted %i alignments" % count)


Converted 1 alignments

The Bio.AlignIO.write() function expects to be given multiple alignment objects. In the example above we gave it the alignment iterator returned by Bio.AlignIO.parse().

In this case, we know there is only one alignment in the file so we could have used Bio.AlignIO.read() instead, but notice we have to pass this alignment to Bio.AlignIO.write() as a single element list:


In [13]:
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")
AlignIO.write([alignment], "PF05371_seed.aln", "clustal")


Out[13]:
1

Either way, you should end up with the same new Clustal W format file “PF05371_seed.aln” with the following content:

CLUSTAL X (1.81) multiple sequence alignment


COATB_BPIKE/30-81                   AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSS
Q9T0Q8_BPIKE/1-52                   AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVS
COATB_BPI22/32-83                   DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSS
COATB_BPM13/24-72                   AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTS
COATB_BPZJ2/1-49                    AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFAS
Q9T0Q9_BPFD/1-49                    AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTS
COATB_BPIF1/22-73                   FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVS

COATB_BPIKE/30-81                   KA
Q9T0Q8_BPIKE/1-52                   RA
COATB_BPI22/32-83                   KA
COATB_BPM13/24-72                   KA
COATB_BPZJ2/1-49                    KA
Q9T0Q9_BPFD/1-49                    KA
COATB_BPIF1/22-73                   RA

Alternatively, you could make a PHYLIP format file which we’ll name “PF05371_seed.phy”:


In [14]:
from Bio import AlignIO
AlignIO.convert("data/PF05371_seed.sth", "stockholm", "PF05371_seed.phy", "phylip")


Out[14]:
1

This time the output looks like this:

7 52
COATB_BPIK AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIRLFKKFSS
Q9T0Q8_BPI AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIKLFKKFVS
COATB_BPI2 DGTSTATSYA TEAMNSLKTQ ATDLIDQTWP VVTSVAVAGL AIRLFKKFSS
COATB_BPM1 AEGDDP---A KAAFNSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
COATB_BPZJ AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFAS
Q9T0Q9_BPF AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
COATB_BPIF FAADDATSQA KAAFDSLTAQ ATEMSGYAWA LVVLVVGATV GIKLFKKFVS

           KA
           RA
           KA
           KA
           KA
           KA
           RA

One of the big handicaps of the original PHYLIP alignment file format is that the sequence identifiers are strictly truncated at ten characters. In this example, as you can see the resulting names are still unique - but they are not very readable. As a result, a more relaxed variant of the original PHYLIP format is now quite widely used:


In [15]:
from Bio import AlignIO
AlignIO.convert("data/PF05371_seed.sth", "stockholm", "PF05371_seed.phy", "phylip-relaxed")


Out[15]:
1

This time the output looks like this, using a longer indentation to allow all the identifers to be given in full::

7 52
COATB_BPIKE/30-81  AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIRLFKKFSS
Q9T0Q8_BPIKE/1-52  AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIKLFKKFVS
COATB_BPI22/32-83  DGTSTATSYA TEAMNSLKTQ ATDLIDQTWP VVTSVAVAGL AIRLFKKFSS
COATB_BPM13/24-72  AEGDDP---A KAAFNSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
COATB_BPZJ2/1-49   AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFAS
Q9T0Q9_BPFD/1-49   AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
COATB_BPIF1/22-73  FAADDATSQA KAAFDSLTAQ ATEMSGYAWA LVVLVVGATV GIKLFKKFVS

                   KA
                   RA
                   KA
                   KA
                   KA
                   KA
                   RA

If you have to work with the original strict PHYLIP format, then you may need to compress the identifers somehow – or assign your own names or numbering system. This following bit of code manipulates the record identifiers before saving the output:


In [16]:
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")
name_mapping = {}
for i, record in enumerate(alignment):
    name_mapping[i] = record.id
    record.id = "seq%i" % i
print(name_mapping)

AlignIO.write([alignment], "PF05371_seed.phy", "phylip")


{0: 'COATB_BPIKE/30-81', 1: 'Q9T0Q8_BPIKE/1-52', 2: 'COATB_BPI22/32-83', 3: 'COATB_BPM13/24-72', 4: 'COATB_BPZJ2/1-49', 5: 'Q9T0Q9_BPFD/1-49', 6: 'COATB_BPIF1/22-73'}
Out[16]:
1

Here is the new (strict) PHYLIP format output:

7 52
seq0       AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIRLFKKFSS
seq1       AEPNAATNYA TEAMDSLKTQ AIDLISQTWP VVTTVVVAGL VIKLFKKFVS
seq2       DGTSTATSYA TEAMNSLKTQ ATDLIDQTWP VVTSVAVAGL AIRLFKKFSS
seq3       AEGDDP---A KAAFNSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
seq4       AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFAS
seq5       AEGDDP---A KAAFDSLQAS ATEYIGYAWA MVVVIVGATI GIKLFKKFTS
seq6       FAADDATSQA KAAFDSLTAQ ATEMSGYAWA LVVLVVGATV GIKLFKKFVS

           KA
           RA
           KA
           KA
           KA
           KA
           RA

In general, because of the identifier limitation, working with strict PHYLIP file formats shouldn’t be your first choice. Using the PFAM/Stockholm format on the other hand allows you to record a lot of additional annotation too.

Getting your alignment objects as formatted strings {#sec:alignment-format-method}

The Bio.AlignIO interface is based on handles, which means if you want to get your alignment(s) into a string in a particular file format you need to do a little bit more work (see below). However, you will probably prefer to take advantage of the alignment object’s format() method. This takes a single mandatory argument, a lower case string which is supported by Bio.AlignIO as an output format. For example:

from Bio import AlignIO
alignment = AlignIO.read("PF05371_seed.sth", "stockholm")
print(alignment.format("clustal"))

As described in Section [sec:SeqRecord-format], the SeqRecord object has a similar method using output formats supported by Bio.SeqIO.

Internally the format() method is using the StringIO string based handle and calling Bio.AlignIO.write(). You can do this in your own code if for example you are using an older version of Biopython:

from Bio import AlignIO
from StringIO import StringIO

alignments = AlignIO.parse("PF05371_seed.sth", "stockholm")

out_handle = StringIO()
AlignIO.write(alignments, out_handle, "clustal")
clustal_data = out_handle.getvalue()

print(clustal_data)

Manipulating Alignments {#sec:manipulating-alignments}

Now that we’ve covered loading and saving alignments, we’ll look at what else you can do with them.

Slicing alignments

First of all, in some senses the alignment objects act like a Python list of SeqRecord objects (the rows). With this model in mind hopefully the actions of len() (the number of rows) and iteration (each row as a SeqRecord) make sense:


In [17]:
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")
print("Number of rows: %i" % len(alignment))


Number of rows: 7

In [18]:
for record in alignment:
    print("%s - %s" % (record.seq, record.id))


AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA - COATB_BPIKE/30-81
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA - Q9T0Q8_BPIKE/1-52
DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA - COATB_BPI22/32-83
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA - COATB_BPM13/24-72
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA - COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA - Q9T0Q9_BPFD/1-49
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA - COATB_BPIF1/22-73

You can also use the list-like append and extend methods to add more rows to the alignment (as SeqRecord objects). Keeping the list metaphor in mind, simple slicing of the alignment should also make sense

  • it selects some of the rows giving back another alignment object:

In [19]:
print(alignment)


SingleLetterAlphabet() alignment with 7 rows and 52 columns
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRL...SKA COATB_BPIKE/30-81
AEPNAATNYATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKL...SRA Q9T0Q8_BPIKE/1-52
DGTSTATSYATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRL...SKA COATB_BPI22/32-83
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPM13/24-72
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA Q9T0Q9_BPFD/1-49
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKL...SRA COATB_BPIF1/22-73

In [20]:
print(alignment[3:7])


SingleLetterAlphabet() alignment with 4 rows and 52 columns
AEGDDP---AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPM13/24-72
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA COATB_BPZJ2/1-49
AEGDDP---AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKL...SKA Q9T0Q9_BPFD/1-49
FAADDATSQAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKL...SRA COATB_BPIF1/22-73

What if you wanted to select by column? Those of you who have used the NumPy matrix or array objects won’t be surprised at this - you use a double index.


In [21]:
print(alignment[2, 6])


T

Using two integer indices pulls out a single letter, short hand for this:


In [22]:
print(alignment[2].seq[6])


T

You can pull out a single column as a string like this:


In [23]:
print(alignment[:, 6])


TTT---T

You can also select a range of columns. For example, to pick out those same three rows we extracted earlier, but take just their first six columns:


In [24]:
print(alignment[3:6, :6])


SingleLetterAlphabet() alignment with 3 rows and 6 columns
AEGDDP COATB_BPM13/24-72
AEGDDP COATB_BPZJ2/1-49
AEGDDP Q9T0Q9_BPFD/1-49

Leaving the first index as : means take all the rows:


In [25]:
print(alignment[:, :6])


SingleLetterAlphabet() alignment with 7 rows and 6 columns
AEPNAA COATB_BPIKE/30-81
AEPNAA Q9T0Q8_BPIKE/1-52
DGTSTA COATB_BPI22/32-83
AEGDDP COATB_BPM13/24-72
AEGDDP COATB_BPZJ2/1-49
AEGDDP Q9T0Q9_BPFD/1-49
FAADDA COATB_BPIF1/22-73

This brings us to a neat way to remove a section. Notice columns 7, 8 and 9 which are gaps in three of the seven sequences:


In [26]:
print(alignment[:, 6:9])


SingleLetterAlphabet() alignment with 7 rows and 3 columns
TNY COATB_BPIKE/30-81
TNY Q9T0Q8_BPIKE/1-52
TSY COATB_BPI22/32-83
--- COATB_BPM13/24-72
--- COATB_BPZJ2/1-49
--- Q9T0Q9_BPFD/1-49
TSQ COATB_BPIF1/22-73

Again, you can slice to get everything after the ninth column:


In [27]:
print(alignment[:, 9:])


SingleLetterAlphabet() alignment with 7 rows and 43 columns
ATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA COATB_BPIKE/30-81
ATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA Q9T0Q8_BPIKE/1-52
ATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA COATB_BPI22/32-83
AKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA COATB_BPM13/24-72
AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA COATB_BPZJ2/1-49
AKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA Q9T0Q9_BPFD/1-49
AKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA COATB_BPIF1/22-73

Now, the interesting thing is that addition of alignment objects works by column. This lets you do this as a way to remove a block of columns:


In [28]:
edited = alignment[:, :6] + alignment[:, 9:]
print(edited)


SingleLetterAlphabet() alignment with 7 rows and 49 columns
AEPNAAATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA COATB_BPIKE/30-81
AEPNAAATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA Q9T0Q8_BPIKE/1-52
DGTSTAATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA COATB_BPI22/32-83
AEGDDPAKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA COATB_BPM13/24-72
AEGDDPAKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA COATB_BPZJ2/1-49
AEGDDPAKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA Q9T0Q9_BPFD/1-49
FAADDAAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA COATB_BPIF1/22-73

Another common use of alignment addition would be to combine alignments for several different genes into a meta-alignment. Watch out though - the identifiers need to match up (see Section [sec:SeqRecord-addition] for how adding SeqRecord objects works). You may find it helpful to first sort the alignment rows alphabetically by id:


In [29]:
edited.sort()
print(edited)


SingleLetterAlphabet() alignment with 7 rows and 49 columns
DGTSTAATEAMNSLKTQATDLIDQTWPVVTSVAVAGLAIRLFKKFSSKA COATB_BPI22/32-83
FAADDAAKAAFDSLTAQATEMSGYAWALVVLVVGATVGIKLFKKFVSRA COATB_BPIF1/22-73
AEPNAAATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIRLFKKFSSKA COATB_BPIKE/30-81
AEGDDPAKAAFNSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA COATB_BPM13/24-72
AEGDDPAKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFASKA COATB_BPZJ2/1-49
AEPNAAATEAMDSLKTQAIDLISQTWPVVTTVVVAGLVIKLFKKFVSRA Q9T0Q8_BPIKE/1-52
AEGDDPAKAAFDSLQASATEYIGYAWAMVVVIVGATIGIKLFKKFTSKA Q9T0Q9_BPFD/1-49

Note that you can only add two alignments together if they have the same number of rows.

Alignments as arrays

Depending on what you are doing, it can be more useful to turn the alignment object into an array of letters – and you can do this with NumPy:


In [30]:
import numpy as np
from Bio import AlignIO
alignment = AlignIO.read("data/PF05371_seed.sth", "stockholm")
align_array = np.array([list(rec) for rec in alignment], np.character)
print("Array shape %i by %i" % align_array.shape)


Array shape 7 by 52

If you will be working heavily with the columns, you can tell NumPy to store the array by column (as in Fortran) rather then its default of by row (as in C):


In [31]:
align_array = np.array([list(rec) for rec in alignment], np.character, order="F")

Note that this leaves the original Biopython alignment object and the NumPy array in memory as separate objects - editing one will not update the other!

Alignment Tools {#sec:alignment-tools}

There are lots of algorithms out there for aligning sequences, both pairwise alignments and multiple sequence alignments. These calculations are relatively slow, and you generally wouldn’t want to write such an algorithm in Python. Instead, you can use Biopython to invoke a command line tool on your behalf. Normally you would:

  1. Prepare an input file of your unaligned sequences, typically this will be a FASTA file which you might create using Bio.SeqIO (see Chapter [chapter:Bio.SeqIO]).

  2. Call the command line tool to process this input file, typically via one of Biopython’s command line wrappers (which we’ll discuss here).

  3. Read the output from the tool, i.e. your aligned sequences, typically using Bio.AlignIO (see earlier in this chapter).

All the command line wrappers we’re going to talk about in this chapter follow the same style. You create a command line object specifying the options (e.g. the input filename and the output filename), then invoke this command line via a Python operating system call (e.g. using the subprocess module).

Most of these wrappers are defined in the Bio.Align.Applications module:


In [32]:
import Bio.Align.Applications
dir(Bio.Align.Applications)


Out[32]:
['ClustalOmegaCommandline',
 'ClustalwCommandline',
 'DialignCommandline',
 'MSAProbsCommandline',
 'MafftCommandline',
 'MuscleCommandline',
 'PrankCommandline',
 'ProbconsCommandline',
 'TCoffeeCommandline',
 '_ClustalOmega',
 '_Clustalw',
 '_Dialign',
 '_MSAProbs',
 '_Mafft',
 '_Muscle',
 '_Prank',
 '_Probcons',
 '_TCoffee',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__docformat__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__']

(Ignore the entries starting with an underscore – these have special meaning in Python.) The module Bio.Emboss.Applications has wrappers for some of the EMBOSS suite, including needle and water, which are described below in Section [seq:emboss-needle-water], and wrappers for the EMBOSS packaged versions of the PHYLIP tools (which EMBOSS refer to as one of their EMBASSY packages - third party tools with an EMBOSS style interface). We won’t explore all these alignment tools here in the section, just a sample, but the same principles apply.

ClustalW {#sec:align_clustal}

ClustalW is a popular command line tool for multiple sequence alignment (there is also a graphical interface called ClustalX). Biopython’s Bio.Align.Applications module has a wrapper for this alignment tool (and several others).

Before trying to use ClustalW from within Python, you should first try running the ClustalW tool yourself by hand at the command line, to familiarise yourself the other options. You’ll find the Biopython wrapper is very faithful to the actual command line API:


In [33]:
from Bio.Align.Applications import ClustalwCommandline
help(ClustalwCommandline)


Help on class ClustalwCommandline in module Bio.Align.Applications._Clustalw:

class ClustalwCommandline(Bio.Application.AbstractCommandline)
 |  Command line wrapper for clustalw (version one or two).
 |  
 |  http://www.clustal.org/
 |  
 |  Example:
 |  --------
 |  
 |  >>> from Bio.Align.Applications import ClustalwCommandline
 |  >>> in_file = "unaligned.fasta"
 |  >>> clustalw_cline = ClustalwCommandline("clustalw2", infile=in_file)
 |  >>> print(clustalw_cline)
 |  clustalw2 -infile=unaligned.fasta
 |  
 |  You would typically run the command line with clustalw_cline() or via
 |  the Python subprocess module, as described in the Biopython tutorial.
 |  
 |  Citation:
 |  ---------
 |  
 |  Larkin MA, Blackshields G, Brown NP, Chenna R, McGettigan PA,
 |  McWilliam H, Valentin F, Wallace IM, Wilm A, Lopez R, Thompson JD,
 |  Gibson TJ, Higgins DG. (2007). Clustal W and Clustal X version 2.0.
 |  Bioinformatics, 23, 2947-2948.
 |  
 |  Last checked against versions: 1.83 and 2.1
 |  
 |  Method resolution order:
 |      ClustalwCommandline
 |      Bio.Application.AbstractCommandline
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, cmd='clustalw', **kwargs)
 |      Create a new instance of a command line wrapper object.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Bio.Application.AbstractCommandline:
 |  
 |  __call__(self, stdin=None, stdout=True, stderr=True, cwd=None, env=None)
 |      Executes the command, waits for it to finish, and returns output.
 |      
 |      Runs the command line tool and waits for it to finish. If it returns
 |      a non-zero error level, an exception is raised. Otherwise two strings
 |      are returned containing stdout and stderr.
 |      
 |      The optional stdin argument should be a string of data which will be
 |      passed to the tool as standard input.
 |      
 |      The optional stdout and stderr argument may be filenames (string),
 |      but otherwise are treated as a booleans, and control if the output
 |      should be captured as strings (True, default), or ignored by sending
 |      it to /dev/null to avoid wasting memory (False). If sent to a file
 |      or ignored, then empty string(s) are returned.
 |      
 |      The optional cwd argument is a string giving the working directory
 |      to run the command from. See Python's subprocess module documentation
 |      for more details.
 |      
 |      The optional env argument is a dictionary setting the environment
 |      variables to be used in the new process. By default the current
 |      process' environment variables are used. See Python's subprocess
 |      module documentation for more details.
 |      
 |      Default example usage::
 |      
 |          from Bio.Emboss.Applications import WaterCommandline
 |          water_cmd = WaterCommandline(gapopen=10, gapextend=0.5,
 |                                       stdout=True, auto=True,
 |                                       asequence="a.fasta", bsequence="b.fasta")
 |          print("About to run: %s" % water_cmd)
 |          std_output, err_output = water_cmd()
 |      
 |      This functionality is similar to subprocess.check_output() added in
 |      Python 2.7. In general if you require more control over running the
 |      command, use subprocess directly.
 |      
 |      As of Biopython 1.56, when the program called returns a non-zero error
 |      level, a custom ApplicationError exception is raised. This includes
 |      any stdout and stderr strings captured as attributes of the exception
 |      object, since they may be useful for diagnosing what went wrong.
 |  
 |  __repr__(self)
 |      Return a representation of the command line object for debugging.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> cline
 |      WaterCommandline(cmd='water', outfile='temp_water.txt', asequence='asis:ACCCGGGCGCGGT', bsequence='asis:ACCCGAGCGCGGT', gapopen=10, gapextend=0.5)
 |  
 |  __setattr__(self, name, value)
 |      Set attribute name to value (PRIVATE).
 |      
 |      This code implements a workaround for a user interface issue.
 |      Without this __setattr__ attribute-based assignment of parameters
 |      will silently accept invalid parameters, leading to known instances
 |      of the user assuming that parameters for the application are set,
 |      when they are not.
 |      
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5, stdout=True)
 |      >>> cline.asequence = "a.fasta"
 |      >>> cline.bsequence = "b.fasta"
 |      >>> cline.csequence = "c.fasta"
 |      Traceback (most recent call last):
 |      ...
 |      ValueError: Option name csequence was not found.
 |      >>> print(cline)
 |      water -stdout -asequence=a.fasta -bsequence=b.fasta -gapopen=10 -gapextend=0.5
 |      
 |      This workaround uses a whitelist of object attributes, and sets the
 |      object attribute list as normal, for these.  Other attributes are
 |      assumed to be parameters, and passed to the self.set_parameter method
 |      for validation and assignment.
 |  
 |  __str__(self)
 |      Make the commandline string with the currently set options.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> str(cline)
 |      'water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5'
 |  
 |  set_parameter(self, name, value=None)
 |      Set a commandline option for a program (OBSOLETE).
 |      
 |      Every parameter is available via a property and as a named
 |      keyword when creating the instance. Using either of these is
 |      preferred to this legacy set_parameter method which is now
 |      OBSOLETE, and likely to be DEPRECATED and later REMOVED in
 |      future releases.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Bio.Application.AbstractCommandline:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from Bio.Application.AbstractCommandline:
 |  
 |  parameters = None

For the most basic usage, all you need is to have a FASTA input file, such as opuntia.fasta (available online or in the Doc/examples subdirectory of the Biopython source code). This is a small FASTA file containing seven prickly-pear DNA sequences (from the cactus family Opuntia).

By default ClustalW will generate an alignment and guide tree file with names based on the input FASTA file, in this case opuntia.aln and opuntia.dnd, but you can override this or make it explicit:


In [34]:
from Bio.Align.Applications import ClustalwCommandline
cline = ClustalwCommandline("clustalw2", infile="data/opuntia.fasta")
print(cline)


clustalw2 -infile=data/opuntia.fasta

Notice here we have given the executable name as clustalw2, indicating we have version two installed, which has a different filename to version one (clustalw, the default). Fortunately both versions support the same set of arguments at the command line (and indeed, should be functionally identical).

You may find that even though you have ClustalW installed, the above command doesn’t work – you may get a message about “command not found” (especially on Windows). This indicated that the ClustalW executable is not on your PATH (an environment variable, a list of directories to be searched). You can either update your PATH setting to include the location of your copy of ClustalW tools (how you do this will depend on your OS), or simply type in the full path of the tool. For example:


In [35]:
import os
from Bio.Align.Applications import ClustalwCommandline
clustalw_exe = r"C:\Program Files\new clustal\clustalw2.exe"
clustalw_cline = ClustalwCommandline(clustalw_exe, infile="data/opuntia.fasta")

In [37]:
assert os.path.isfile(clustalw_exe), "Clustal W executable missing"
stdout, stderr = clustalw_cline()


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-37-2132fefeb91f> in <module>()
----> 1 assert os.path.isfile(clustalw_exe), "Clustal W executable missing"
      2 stdout, stderr = clustalw_cline()

AssertionError: Clustal W executable missing

Remember, in Python strings \n and \t are by default interpreted as a new line and a tab – which is why we’re put a letter “r” at the start for a raw string that isn’t translated in this way. This is generally good practice when specifying a Windows style file name.

Internally this uses the subprocess module which is now the recommended way to run another program in Python. This replaces older options like the os.system() and the os.popen* functions.

Now, at this point it helps to know about how command line tools “work”. When you run a tool at the command line, it will often print text output directly to screen. This text can be captured or redirected, via two “pipes”, called standard output (the normal results) and standard error (for error messages and debug messages). There is also standard input, which is any text fed into the tool. These names get shortened to stdin, stdout and stderr. When the tool finishes, it has a return code (an integer), which by convention is zero for success.

When you run the command line tool like this via the Biopython wrapper, it will wait for it to finish, and check the return code. If this is non zero (indicating an error), an exception is raised. The wrapper then returns two strings, stdout and stderr.

In the case of ClustalW, when run at the command line all the important output is written directly to the output files. Everything normally printed to screen while you wait (via stdout or stderr) is boring and can be ignored (assuming it worked).

What we care about are the two output files, the alignment and the guide tree. We didn’t tell ClustalW what filenames to use, but it defaults to picking names based on the input file. In this case the output should be in the file opuntia.aln. You should be able to work out how to read in the alignment using Bio.AlignIO by now:


In [38]:
from Bio import AlignIO
align = AlignIO.read("data/opuntia.aln", "clustal")
print(align)


SingleLetterAlphabet() alignment with 7 rows and 906 columns
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273285|gb|AF191659.1|AF191
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273284|gb|AF191658.1|AF191
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273287|gb|AF191661.1|AF191
TATACATAAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273286|gb|AF191660.1|AF191
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273290|gb|AF191664.1|AF191
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273289|gb|AF191663.1|AF191
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273291|gb|AF191665.1|AF191

In case you are interested (and this is an aside from the main thrust of this chapter), the opuntia.dnd file ClustalW creates is just a standard Newick tree file, and Bio.Phylo can parse these:


In [39]:
from Bio import Phylo
tree = Phylo.read("data/opuntia.dnd", "newick")
Phylo.draw_ascii(tree)


                             _______________ gi|6273291|gb|AF191665.1|AF191665
  __________________________|
 |                          |   ______ gi|6273290|gb|AF191664.1|AF191664
 |                          |__|
 |                             |_____ gi|6273289|gb|AF191663.1|AF191663
 |
_|_________________ gi|6273287|gb|AF191661.1|AF191661
 |
 |__________ gi|6273286|gb|AF191660.1|AF191660
 |
 |    __ gi|6273285|gb|AF191659.1|AF191659
 |___|
     | gi|6273284|gb|AF191658.1|AF191658

Chapter [sec:Phylo] covers Biopython’s support for phylogenetic trees in more depth.

MUSCLE

MUSCLE is a more recent multiple sequence alignment tool than ClustalW, and Biopython also has a wrapper for it under the Bio.Align.Applications module. As before, we recommend you try using MUSCLE from the command line before trying it from within Python, as the Biopython wrapper is very faithful to the actual command line API:


In [40]:
from Bio.Align.Applications import MuscleCommandline
help(MuscleCommandline)


Help on class MuscleCommandline in module Bio.Align.Applications._Muscle:

class MuscleCommandline(Bio.Application.AbstractCommandline)
 |  Command line wrapper for the multiple alignment program MUSCLE.
 |  
 |  http://www.drive5.com/muscle/
 |  
 |  Example:
 |  --------
 |  
 |  >>> from Bio.Align.Applications import MuscleCommandline
 |  >>> muscle_exe = r"C:\Program Files\Aligments\muscle3.8.31_i86win32.exe"
 |  >>> in_file = r"C:\My Documents\unaligned.fasta"
 |  >>> out_file = r"C:\My Documents\aligned.fasta"
 |  >>> muscle_cline = MuscleCommandline(muscle_exe, input=in_file, out=out_file)
 |  >>> print(muscle_cline)
 |  "C:\Program Files\Aligments\muscle3.8.31_i86win32.exe" -in "C:\My Documents\unaligned.fasta" -out "C:\My Documents\aligned.fasta"
 |  
 |  You would typically run the command line with muscle_cline() or via
 |  the Python subprocess module, as described in the Biopython tutorial.
 |  
 |  Citations:
 |  ----------
 |  
 |  Edgar, Robert C. (2004), MUSCLE: multiple sequence alignment with high
 |  accuracy and high throughput, Nucleic Acids Research 32(5), 1792-97.
 |  
 |  Edgar, R.C. (2004) MUSCLE: a multiple sequence alignment method with
 |  reduced time and space complexity. BMC Bioinformatics 5(1): 113.
 |  
 |  Last checked against version: 3.7, briefly against 3.8
 |  
 |  Method resolution order:
 |      MuscleCommandline
 |      Bio.Application.AbstractCommandline
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, cmd='muscle', **kwargs)
 |      Create a new instance of a command line wrapper object.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Bio.Application.AbstractCommandline:
 |  
 |  __call__(self, stdin=None, stdout=True, stderr=True, cwd=None, env=None)
 |      Executes the command, waits for it to finish, and returns output.
 |      
 |      Runs the command line tool and waits for it to finish. If it returns
 |      a non-zero error level, an exception is raised. Otherwise two strings
 |      are returned containing stdout and stderr.
 |      
 |      The optional stdin argument should be a string of data which will be
 |      passed to the tool as standard input.
 |      
 |      The optional stdout and stderr argument may be filenames (string),
 |      but otherwise are treated as a booleans, and control if the output
 |      should be captured as strings (True, default), or ignored by sending
 |      it to /dev/null to avoid wasting memory (False). If sent to a file
 |      or ignored, then empty string(s) are returned.
 |      
 |      The optional cwd argument is a string giving the working directory
 |      to run the command from. See Python's subprocess module documentation
 |      for more details.
 |      
 |      The optional env argument is a dictionary setting the environment
 |      variables to be used in the new process. By default the current
 |      process' environment variables are used. See Python's subprocess
 |      module documentation for more details.
 |      
 |      Default example usage::
 |      
 |          from Bio.Emboss.Applications import WaterCommandline
 |          water_cmd = WaterCommandline(gapopen=10, gapextend=0.5,
 |                                       stdout=True, auto=True,
 |                                       asequence="a.fasta", bsequence="b.fasta")
 |          print("About to run: %s" % water_cmd)
 |          std_output, err_output = water_cmd()
 |      
 |      This functionality is similar to subprocess.check_output() added in
 |      Python 2.7. In general if you require more control over running the
 |      command, use subprocess directly.
 |      
 |      As of Biopython 1.56, when the program called returns a non-zero error
 |      level, a custom ApplicationError exception is raised. This includes
 |      any stdout and stderr strings captured as attributes of the exception
 |      object, since they may be useful for diagnosing what went wrong.
 |  
 |  __repr__(self)
 |      Return a representation of the command line object for debugging.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> cline
 |      WaterCommandline(cmd='water', outfile='temp_water.txt', asequence='asis:ACCCGGGCGCGGT', bsequence='asis:ACCCGAGCGCGGT', gapopen=10, gapextend=0.5)
 |  
 |  __setattr__(self, name, value)
 |      Set attribute name to value (PRIVATE).
 |      
 |      This code implements a workaround for a user interface issue.
 |      Without this __setattr__ attribute-based assignment of parameters
 |      will silently accept invalid parameters, leading to known instances
 |      of the user assuming that parameters for the application are set,
 |      when they are not.
 |      
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5, stdout=True)
 |      >>> cline.asequence = "a.fasta"
 |      >>> cline.bsequence = "b.fasta"
 |      >>> cline.csequence = "c.fasta"
 |      Traceback (most recent call last):
 |      ...
 |      ValueError: Option name csequence was not found.
 |      >>> print(cline)
 |      water -stdout -asequence=a.fasta -bsequence=b.fasta -gapopen=10 -gapextend=0.5
 |      
 |      This workaround uses a whitelist of object attributes, and sets the
 |      object attribute list as normal, for these.  Other attributes are
 |      assumed to be parameters, and passed to the self.set_parameter method
 |      for validation and assignment.
 |  
 |  __str__(self)
 |      Make the commandline string with the currently set options.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> str(cline)
 |      'water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5'
 |  
 |  set_parameter(self, name, value=None)
 |      Set a commandline option for a program (OBSOLETE).
 |      
 |      Every parameter is available via a property and as a named
 |      keyword when creating the instance. Using either of these is
 |      preferred to this legacy set_parameter method which is now
 |      OBSOLETE, and likely to be DEPRECATED and later REMOVED in
 |      future releases.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Bio.Application.AbstractCommandline:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from Bio.Application.AbstractCommandline:
 |  
 |  parameters = None

For the most basic usage, all you need is to have a FASTA input file, such as opuntia.fasta (available online or in the Doc/examples subdirectory of the Biopython source code). You can then tell MUSCLE to read in this FASTA file, and write the alignment to an output file:


In [41]:
from Bio.Align.Applications import MuscleCommandline
cline = MuscleCommandline(input="data/opuntia.fasta", out="opuntia.txt")
print(cline)


muscle -in data/opuntia.fasta -out opuntia.txt

Note that MUSCLE uses “-in” and “-out” but in Biopython we have to use “input” and “out” as the keyword arguments or property names. This is because “in” is a reserved word in Python.

By default MUSCLE will output the alignment as a FASTA file (using gapped sequences). The Bio.AlignIO module should be able to read this alignment using format=fasta. You can also ask for ClustalW-like output:


In [42]:
from Bio.Align.Applications import MuscleCommandline
cline = MuscleCommandline(input="data/opuntia.fasta", out="opuntia.aln", clw=True)
print(cline)


muscle -in data/opuntia.fasta -out opuntia.aln -clw

Or, strict ClustalW output where the original ClustalW header line is used for maximum compatibility:


In [43]:
from Bio.Align.Applications import MuscleCommandline
cline = MuscleCommandline(input="data/opuntia.fasta", out="opuntia.aln", clwstrict=True)
print(cline)


muscle -in data/opuntia.fasta -out opuntia.aln -clwstrict

The Bio.AlignIO module should be able to read these alignments using format=clustal.

MUSCLE can also output in GCG MSF format (using the msf argument), but Biopython can’t currently parse that, or using HTML which would give a human readable web page (not suitable for parsing).

You can also set the other optional parameters, for example the maximum number of iterations. See the built in help for details.

You would then run MUSCLE command line string as described above for ClustalW, and parse the output using Bio.AlignIO to get an alignment object.

MUSCLE using stdout

Using a MUSCLE command line as in the examples above will write the alignment to a file. This means there will be no important information written to the standard out (stdout) or standard error (stderr) handles. However, by default MUSCLE will write the alignment to standard output (stdout). We can take advantage of this to avoid having a temporary output file! For example:


In [44]:
from Bio.Align.Applications import MuscleCommandline
muscle_cline = MuscleCommandline(input="data/opuntia.fasta")
print(muscle_cline)


muscle -in data/opuntia.fasta

If we run this via the wrapper, we get back the output as a string. In order to parse this we can use StringIO to turn it into a handle. Remember that MUSCLE defaults to using FASTA as the output format:


In [45]:
from Bio.Align.Applications import MuscleCommandline
muscle_cline = MuscleCommandline(input="data/opuntia.fasta")
stdout, stderr = muscle_cline()
from io import StringIO
from Bio import AlignIO
align = AlignIO.read(StringIO(stdout), "fasta")
print(align)


SingleLetterAlphabet() alignment with 7 rows and 906 columns
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273289|gb|AF191663.1|AF191663
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273291|gb|AF191665.1|AF191665
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273290|gb|AF191664.1|AF191664
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273287|gb|AF191661.1|AF191661
TATACATAAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273286|gb|AF191660.1|AF191660
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273285|gb|AF191659.1|AF191659
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273284|gb|AF191658.1|AF191658

The above approach is fairly simple, but if you are dealing with very large output text the fact that all of stdout and stderr is loaded into memory as a string can be a potential drawback. Using the subprocess module we can work directly with handles instead:


In [49]:
import subprocess
import sys
from Bio.Align.Applications import MuscleCommandline
muscle_cline = MuscleCommandline(input="data/opuntia.fasta")
child = subprocess.Popen(str(muscle_cline),
    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    shell=(sys.platform != "win32"),
    universal_newlines=True)
from Bio import AlignIO
align = AlignIO.read(child.stdout, "fasta")
print(align)


SingleLetterAlphabet() alignment with 7 rows and 906 columns
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273289|gb|AF191663.1|AF191663
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273291|gb|AF191665.1|AF191665
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273290|gb|AF191664.1|AF191664
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273287|gb|AF191661.1|AF191661
TATACATAAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273286|gb|AF191660.1|AF191660
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273285|gb|AF191659.1|AF191659
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273284|gb|AF191658.1|AF191658

MUSCLE using stdin and stdout

We don’t actually need to have our FASTA input sequences prepared in a file, because by default MUSCLE will read in the input sequence from standard input! Note this is a bit more advanced and fiddly, so don’t bother with this technique unless you need to.

First, we’ll need some unaligned sequences in memory as SeqRecord objects. For this demonstration I’m going to use a filtered version of the original FASTA file (using a generator expression), taking just six of the seven sequences:


In [73]:
from Bio import SeqIO
records = (r for r in SeqIO.parse("data/opuntia.fasta", "fasta") if len(r) < 900)

Then we create the MUSCLE command line, leaving the input and output to their defaults (stdin and stdout). I’m also going to ask for strict ClustalW format as for the output.


In [90]:
from Bio.Align.Applications import MuscleCommandline
muscle_cline = MuscleCommandline(clwstrict=True)
print(muscle_cline)


muscle -clwstrict

Now for the fiddly bits using the subprocess module, stdin and stdout:


In [91]:
import subprocess
import sys
child = subprocess.Popen(str(cline),
    stdin=subprocess.PIPE, stdout=subprocess.PIPE,
    stderr=subprocess.PIPE, universal_newlines=True,
    shell=(sys.platform != "win32"))

That should start MUSCLE, but it will be sitting waiting for its FASTA input sequences, which we must supply via its stdin handle:


In [92]:
SeqIO.write(records, child.stdin, "fasta")


---------------------------------------------------------------------------
BrokenPipeError                           Traceback (most recent call last)
<ipython-input-92-11690e1b720b> in <module>()
----> 1 SeqIO.write(records, child.stdin, "fasta")

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/SeqIO/__init__.py in write(sequences, handle, format)
    481         if format in _FormatToWriter:
    482             writer_class = _FormatToWriter[format]
--> 483             count = writer_class(fp).write_file(sequences)
    484         elif format in AlignIO._FormatToWriter:
    485             # Try and turn all the records into a single alignment,

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/SeqIO/Interfaces.py in write_file(self, records)
    209         """
    210         self.write_header()
--> 211         count = self.write_records(records)
    212         self.write_footer()
    213         return count

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/SeqIO/Interfaces.py in write_records(self, records)
    194         count = 0
    195         for record in records:
--> 196             self.write_record(record)
    197             count += 1
    198         # Mark as true, even if there where no records

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/SeqIO/FastaIO.py in write_record(self, record)
    209         if self.wrap:
    210             for i in range(0, len(data), self.wrap):
--> 211                 self.handle.write(data[i:i + self.wrap] + "\n")
    212         else:
    213             self.handle.write(data + "\n")

BrokenPipeError: [Errno 32] Broken pipe

In [93]:
child.stdin.close()


---------------------------------------------------------------------------
BrokenPipeError                           Traceback (most recent call last)
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

BrokenPipeError                           Traceback (most recent call last)
<ipython-input-93-f5a7c02c7fa3> in <module>()
----> 1 child.stdin.close()

BrokenPipeError: [Errno 32] Broken pipe

After writing the six sequences to the handle, MUSCLE will still be waiting to see if that is all the FASTA sequences or not – so we must signal that this is all the input data by closing the handle. At that point MUSCLE should start to run, and we can ask for the output:


In [94]:
from Bio import AlignIO
align = AlignIO.read(child.stdout, "clustal")
print(align)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-94-240021347a83> in <module>()
      1 from Bio import AlignIO
----> 2 align = AlignIO.read(child.stdout, "clustal")
      3 print(align)

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py in read(handle, format, seq_count, alphabet)
    429         first = None
    430     if first is None:
--> 431         raise ValueError("No records found in handle")
    432     try:
    433         second = next(iterator)

ValueError: No records found in handle

Wow! There we are with a new alignment of just the six records, without having created a temporary FASTA input file, or a temporary alignment output file. However, a word of caution: Dealing with errors with this style of calling external programs is much more complicated. It also becomes far harder to diagnose problems, because you can’t try running MUSCLE manually outside of Biopython (because you don’t have the input file to supply). There can also be subtle cross platform issues (e.g. Windows versus Linux, Python 2 versus Python 3), and how you run your script can have an impact (e.g. at the command line, from IDLE or an IDE, or as a GUI script). These are all generic Python issues though, and not specific to Biopython.

If you find working directly with subprocess like this scary, there is an alternative. If you execute the tool with muscle_cline() you can supply any standard input as a big string, muscle_cline(stdin=...). So, provided your data isn’t very big, you can prepare the FASTA input in memory as a string using StringIO (see Section [sec:appendix-handles]):


In [95]:
from Bio import SeqIO
records = (r for r in SeqIO.parse("data/opuntia.fasta", "fasta") if len(r) < 900)
handle = StringIO()
SeqIO.write(records, handle, "fasta")


Out[95]:
6

In [96]:
data = handle.getvalue()

You can then run the tool and parse the alignment as follows:


In [97]:
stdout, stderr = muscle_cline(stdin=data)
from Bio import AlignIO
align = AlignIO.read(StringIO(stdout), "clustal")
print(align)


SingleLetterAlphabet() alignment with 6 rows and 900 columns
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273290|gb|AF191664.1|AF19166
TATACATTAAAGGAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273289|gb|AF191663.1|AF19166
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273287|gb|AF191661.1|AF19166
TATACATAAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273286|gb|AF191660.1|AF19166
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273285|gb|AF191659.1|AF19165
TATACATTAAAGAAGGGGGATGCGGATAAATGGAAAGGCGAAAG...AGA gi|6273284|gb|AF191658.1|AF19165

You might find this easier, but it does require more memory (RAM) for the strings used for the input FASTA and output Clustal formatted data.

EMBOSS needle and water {#seq:emboss-needle-water}

The EMBOSS suite includes the water and needle tools for Smith-Waterman algorithm local alignment, and Needleman-Wunsch global alignment. The tools share the same style interface, so switching between the two is trivial – we’ll just use needle here.

Suppose you want to do a global pairwise alignment between two sequences, prepared in FASTA format as follows:

>HBA_HUMAN
MVLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLSHGSAQVKGHG
KKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLPAEFTP
AVHASLDKFLASVSTVLTSKYR

in a file alpha.fasta, and secondly in a file beta.fasta:

>HBB_HUMAN
MVHLTPEEKSAVTALWGKVNVDEVGGEALGRLLVVYPWTQRFFESFGDLSTPDAVMGNPK
VKAHGKKVLGAFSDGLAHLDNLKGTFATLSELHCDKLHVDPENFRLLGNVLVCVLAHHFG
KEFTPPVQAAYQKVVAGVANALAHKYH

Let’s start by creating a complete needle command line object in one go:


In [98]:
from Bio.Emboss.Applications import NeedleCommandline
needle_cline = NeedleCommandline(asequence="data/alpha.faa", bsequence="data/beta.faa",
    gapopen=10, gapextend=0.5, outfile="needle.txt")
print(needle_cline)


needle -outfile=needle.txt -asequence=data/alpha.faa -bsequence=data/beta.faa -gapopen=10 -gapextend=0.5

Why not try running this by hand at the command prompt? You should see it does a pairwise comparison and records the output in the file needle.txt (in the default EMBOSS alignment file format).

Even if you have EMBOSS installed, running this command may not work – you might get a message about “command not found” (especially on Windows). This probably means that the EMBOSS tools are not on your PATH environment variable. You can either update your PATH setting, or simply tell Biopython the full path to the tool, for example:


In [99]:
from Bio.Emboss.Applications import NeedleCommandline
needle_cline = NeedleCommandline(r"C:\EMBOSS\needle.exe",
    asequence="data/alpha.faa", bsequence="data/beta.faa",
    gapopen=10, gapextend=0.5, outfile="needle.txt")

Remember in Python that for a default string \n or \t means a new line or a tab – which is why we’re put a letter “r” at the start for a raw string.

At this point it might help to try running the EMBOSS tools yourself by hand at the command line, to familiarise yourself the other options and compare them to the Biopython help text:


In [100]:
from Bio.Emboss.Applications import NeedleCommandline
help(NeedleCommandline)


Help on class NeedleCommandline in module Bio.Emboss.Applications:

class NeedleCommandline(_EmbossCommandLine)
 |  Commandline object for the needle program from EMBOSS.
 |  
 |  Method resolution order:
 |      NeedleCommandline
 |      _EmbossCommandLine
 |      _EmbossMinimalCommandLine
 |      Bio.Application.AbstractCommandline
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, cmd='needle', **kwargs)
 |      Create a new instance of a command line wrapper object.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  aformat
 |      Display output in a different specified output format
 |      
 |      This controls the addition of the -aformat parameter and its associated value.  Set this property to the argument value required.
 |  
 |  asequence
 |      First sequence to align
 |      
 |      This controls the addition of the -asequence parameter and its associated value.  Set this property to the argument value required.
 |  
 |  auto
 |      Turn off prompts.
 |      
 |                         Automatic mode disables prompting, so we recommend you set
 |                         this argument all the time when calling an EMBOSS tool from
 |                         Biopython.
 |                         
 |      
 |      This property controls the addition of the -auto switch, treat this property as a boolean.
 |  
 |  brief
 |      Display brief identity and similarity
 |      
 |      This property controls the addition of the -brief switch, treat this property as a boolean.
 |  
 |  bsequence
 |      Second sequence to align
 |      
 |      This controls the addition of the -bsequence parameter and its associated value.  Set this property to the argument value required.
 |  
 |  datafile
 |      Matrix file
 |      
 |      This controls the addition of the -datafile parameter and its associated value.  Set this property to the argument value required.
 |  
 |  debug
 |      Write debug output to program.dbg.
 |      
 |      This property controls the addition of the -debug switch, treat this property as a boolean.
 |  
 |  die
 |      Report dying program messages.
 |      
 |      This property controls the addition of the -die switch, treat this property as a boolean.
 |  
 |  endextend
 |      The score added to the end gap penality for each base or residue in the end gap.
 |      
 |      This controls the addition of the -endextend parameter and its associated value.  Set this property to the argument value required.
 |  
 |  endopen
 |      The score taken away when an end gap is created.
 |      
 |      This controls the addition of the -endopen parameter and its associated value.  Set this property to the argument value required.
 |  
 |  endweight
 |      Apply And gap penalties
 |      
 |      This controls the addition of the -endweight parameter and its associated value.  Set this property to the argument value required.
 |  
 |  error
 |      Report errors.
 |      
 |      This property controls the addition of the -error switch, treat this property as a boolean.
 |  
 |  filter
 |      Read standard input, write standard output.
 |      
 |      This property controls the addition of the -filter switch, treat this property as a boolean.
 |  
 |  gapextend
 |      Gap extension penalty
 |      
 |      This controls the addition of the -gapextend parameter and its associated value.  Set this property to the argument value required.
 |  
 |  gapopen
 |      Gap open penalty
 |      
 |      This controls the addition of the -gapopen parameter and its associated value.  Set this property to the argument value required.
 |  
 |  help
 |      Report command line options.
 |      
 |                         More information on associated and general qualifiers can
 |                         be found with -help -verbose
 |                         
 |      
 |      This property controls the addition of the -help switch, treat this property as a boolean.
 |  
 |  nobrief
 |      Display extended identity and similarity
 |      
 |      This property controls the addition of the -nobrief switch, treat this property as a boolean.
 |  
 |  options
 |      Prompt for standard and additional values.
 |      
 |                         If you are calling an EMBOSS tool from within Biopython,
 |                         we DO NOT recommend using this option.
 |                         
 |      
 |      This property controls the addition of the -options switch, treat this property as a boolean.
 |  
 |  outfile
 |      Output filename
 |      
 |      This controls the addition of the -outfile parameter and its associated value.  Set this property to the argument value required.
 |  
 |  similarity
 |      Display percent identity and similarity
 |      
 |      This controls the addition of the -similarity parameter and its associated value.  Set this property to the argument value required.
 |  
 |  snucleotide
 |      Sequences are nucleotide (boolean)
 |      
 |      This controls the addition of the -snucleotide parameter and its associated value.  Set this property to the argument value required.
 |  
 |  sprotein
 |      Sequences are protein (boolean)
 |      
 |      This controls the addition of the -sprotein parameter and its associated value.  Set this property to the argument value required.
 |  
 |  stdout
 |      Write standard output.
 |      
 |      This property controls the addition of the -stdout switch, treat this property as a boolean.
 |  
 |  verbose
 |      Report some/full command line options
 |      
 |      This property controls the addition of the -verbose switch, treat this property as a boolean.
 |  
 |  warning
 |      Report warnings.
 |      
 |      This property controls the addition of the -warning switch, treat this property as a boolean.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from Bio.Application.AbstractCommandline:
 |  
 |  __call__(self, stdin=None, stdout=True, stderr=True, cwd=None, env=None)
 |      Executes the command, waits for it to finish, and returns output.
 |      
 |      Runs the command line tool and waits for it to finish. If it returns
 |      a non-zero error level, an exception is raised. Otherwise two strings
 |      are returned containing stdout and stderr.
 |      
 |      The optional stdin argument should be a string of data which will be
 |      passed to the tool as standard input.
 |      
 |      The optional stdout and stderr argument may be filenames (string),
 |      but otherwise are treated as a booleans, and control if the output
 |      should be captured as strings (True, default), or ignored by sending
 |      it to /dev/null to avoid wasting memory (False). If sent to a file
 |      or ignored, then empty string(s) are returned.
 |      
 |      The optional cwd argument is a string giving the working directory
 |      to run the command from. See Python's subprocess module documentation
 |      for more details.
 |      
 |      The optional env argument is a dictionary setting the environment
 |      variables to be used in the new process. By default the current
 |      process' environment variables are used. See Python's subprocess
 |      module documentation for more details.
 |      
 |      Default example usage::
 |      
 |          from Bio.Emboss.Applications import WaterCommandline
 |          water_cmd = WaterCommandline(gapopen=10, gapextend=0.5,
 |                                       stdout=True, auto=True,
 |                                       asequence="a.fasta", bsequence="b.fasta")
 |          print("About to run: %s" % water_cmd)
 |          std_output, err_output = water_cmd()
 |      
 |      This functionality is similar to subprocess.check_output() added in
 |      Python 2.7. In general if you require more control over running the
 |      command, use subprocess directly.
 |      
 |      As of Biopython 1.56, when the program called returns a non-zero error
 |      level, a custom ApplicationError exception is raised. This includes
 |      any stdout and stderr strings captured as attributes of the exception
 |      object, since they may be useful for diagnosing what went wrong.
 |  
 |  __repr__(self)
 |      Return a representation of the command line object for debugging.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> cline
 |      WaterCommandline(cmd='water', outfile='temp_water.txt', asequence='asis:ACCCGGGCGCGGT', bsequence='asis:ACCCGAGCGCGGT', gapopen=10, gapextend=0.5)
 |  
 |  __setattr__(self, name, value)
 |      Set attribute name to value (PRIVATE).
 |      
 |      This code implements a workaround for a user interface issue.
 |      Without this __setattr__ attribute-based assignment of parameters
 |      will silently accept invalid parameters, leading to known instances
 |      of the user assuming that parameters for the application are set,
 |      when they are not.
 |      
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5, stdout=True)
 |      >>> cline.asequence = "a.fasta"
 |      >>> cline.bsequence = "b.fasta"
 |      >>> cline.csequence = "c.fasta"
 |      Traceback (most recent call last):
 |      ...
 |      ValueError: Option name csequence was not found.
 |      >>> print(cline)
 |      water -stdout -asequence=a.fasta -bsequence=b.fasta -gapopen=10 -gapextend=0.5
 |      
 |      This workaround uses a whitelist of object attributes, and sets the
 |      object attribute list as normal, for these.  Other attributes are
 |      assumed to be parameters, and passed to the self.set_parameter method
 |      for validation and assignment.
 |  
 |  __str__(self)
 |      Make the commandline string with the currently set options.
 |      
 |      e.g.
 |      >>> from Bio.Emboss.Applications import WaterCommandline
 |      >>> cline = WaterCommandline(gapopen=10, gapextend=0.5)
 |      >>> cline.asequence = "asis:ACCCGGGCGCGGT"
 |      >>> cline.bsequence = "asis:ACCCGAGCGCGGT"
 |      >>> cline.outfile = "temp_water.txt"
 |      >>> print(cline)
 |      water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5
 |      >>> str(cline)
 |      'water -outfile=temp_water.txt -asequence=asis:ACCCGGGCGCGGT -bsequence=asis:ACCCGAGCGCGGT -gapopen=10 -gapextend=0.5'
 |  
 |  set_parameter(self, name, value=None)
 |      Set a commandline option for a program (OBSOLETE).
 |      
 |      Every parameter is available via a property and as a named
 |      keyword when creating the instance. Using either of these is
 |      preferred to this legacy set_parameter method which is now
 |      OBSOLETE, and likely to be DEPRECATED and later REMOVED in
 |      future releases.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from Bio.Application.AbstractCommandline:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from Bio.Application.AbstractCommandline:
 |  
 |  parameters = None

Note that you can also specify (or change or look at) the settings like this:


In [101]:
from Bio.Emboss.Applications import NeedleCommandline
needle_cline = NeedleCommandline()
needle_cline.asequence="data/alpha.faa"
needle_cline.bsequence="data/beta.faa"
needle_cline.gapopen=10
needle_cline.gapextend=0.5
needle_cline.outfile="needle.txt"
print(needle_cline)


needle -outfile=needle.txt -asequence=data/alpha.faa -bsequence=data/beta.faa -gapopen=10 -gapextend=0.5

In [102]:
print(needle_cline.outfile)


needle.txt

Next we want to use Python to run this command for us. As explained above, for full control, we recommend you use the built in Python subprocess module, but for simple usage the wrapper object usually suffices:


In [103]:
stdout, stderr = needle_cline()
print(stdout + stderr)


---------------------------------------------------------------------------
ApplicationError                          Traceback (most recent call last)
<ipython-input-103-c1c0502efb05> in <module>()
----> 1 stdout, stderr = needle_cline()
      2 print(stdout + stderr)

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/Application/__init__.py in __call__(self, stdin, stdout, stderr, cwd, env)
    515         if return_code:
    516             raise ApplicationError(return_code, str(self),
--> 517                                    stdout_str, stderr_str)
    518         return stdout_str, stderr_str
    519 

ApplicationError: Non-zero return code 127 from 'needle -outfile=needle.txt -asequence=data/alpha.faa -bsequence=data/beta.faa -gapopen=10 -gapextend=0.5', message '/bin/sh: 1: needle: not found'

Next we can load the output file with Bio.AlignIO as discussed earlier in this chapter, as the emboss format:


In [104]:
from Bio import AlignIO
align = AlignIO.read("needle.txt", "emboss")
print(align)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-104-cb0b4e27470d> in <module>()
      1 from Bio import AlignIO
----> 2 align = AlignIO.read("needle.txt", "emboss")
      3 print(align)

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py in read(handle, format, seq_count, alphabet)
    425     iterator = parse(handle, format, seq_count, alphabet)
    426     try:
--> 427         first = next(iterator)
    428     except StopIteration:
    429         first = None

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/AlignIO/__init__.py in parse(handle, format, seq_count, alphabet)
    348         raise TypeError("Need integer for seq_count (sequences per alignment)")
    349 
--> 350     with as_handle(handle, 'rU') as fp:
    351         # Map the file format to a sequence iterator:
    352         if format in _FormatToIterator:

/home/tiago_antao/miniconda/lib/python3.5/contextlib.py in __enter__(self)
     57     def __enter__(self):
     58         try:
---> 59             return next(self.gen)
     60         except StopIteration:
     61             raise RuntimeError("generator didn't yield") from None

/home/tiago_antao/miniconda/lib/python3.5/site-packages/Bio/File.py in as_handle(handleish, mode, **kwargs)
     88                 yield fp
     89         else:
---> 90             with open(handleish, mode, **kwargs) as fp:
     91                 yield fp
     92     else:

FileNotFoundError: [Errno 2] No such file or directory: 'needle.txt'

In [ ]: